<template>
  <div class="home-container">
    <!-- 全屏背景 -->
    <div class="background-animation"></div>
    
    <el-container class="main-container">
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '220px'" class="sidebar">
        <div class="logo" :class="{ 'collapsed': isCollapse }">
          <transition name="fade" mode="out-in">
            <img 
              v-if="!isCollapse" 
              src="../assets/logo.svg" 
              alt="Logo" 
              class="logo-image"
              @error="handleLogoError"
            />
          </transition>
          <transition name="fade" mode="out-in">
            <span v-if="!isCollapse" class="logo-text">IoT 智能家居</span>
          </transition>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          :collapse-transition="false"
          background-color="rgba(48, 65, 86, 0.8)"
          text-color="#bfcbd9"
          active-text-color="#409eff"
          unique-opened
          router
        >
          <el-menu-item index="/home">
            <el-icon><House /></el-icon>
            <template #title>
              <span class="menu-title">系统首页</span>
            </template>
          </el-menu-item>
          
          <el-menu-item index="/devices">
            <el-icon><Monitor /></el-icon>
            <template #title>
              <span class="menu-title">设备管理</span>
            </template>
          </el-menu-item>
          
          <el-menu-item index="/rooms">
            <el-icon><HomeFilled /></el-icon>
            <template #title>
              <span class="menu-title">房间管理</span>
            </template>
          </el-menu-item>
          
          <el-menu-item index="/scenes">
            <el-icon><Collection /></el-icon>
            <template #title>
              <span class="menu-title">场景管理</span>
            </template>
          </el-menu-item>
          
          <el-menu-item index="/alarms">
            <el-icon><Bell /></el-icon>
            <template #title>
              <span class="menu-title">报警信息</span>
            </template>
          </el-menu-item>
          
          <el-menu-item index="/analytics">
            <el-icon><DataAnalysis /></el-icon>
            <template #title>
              <span class="menu-title">数据分析</span>
            </template>
          </el-menu-item>
          
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <template #title>
              <span class="menu-title">系统设置</span>
            </template>
          </el-menu-item>
        </el-menu>
        
        <!-- 侧边栏底部信息 -->
        <div class="sidebar-footer" v-if="!isCollapse">
          <div class="system-info">
            <div class="info-item">
              <el-icon><Connection /></el-icon>
              <span>在线设备: {{ onlineDevices }}</span>
            </div>
            <div class="info-item">
              <el-icon><Warning /></el-icon>
              <span>报警: {{ alarmCount }}</span>
            </div>
          </div>
        </div>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container class="content-container">
        <!-- 头部 -->
        <el-header class="header">
          <div class="header-left">
            <el-button 
              class="menu-toggle" 
              @click="toggleCollapse" 
              circle 
              plain
            >
              <el-icon class="toggle-icon">
                <Expand v-if="isCollapse" />
                <Fold v-else />
              </el-icon>
            </el-button>
            
            <div class="breadcrumb">
              <el-breadcrumb separator="/">
                <el-breadcrumb-item 
                  v-for="(item, index) in breadcrumbItems" 
                  :key="index"
                >
                  {{ item }}
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </div>
          
          <div class="header-right">
            <!-- 系统通知 -->
            <div class="notification-area">
              <el-badge :value="alarmCount" :max="99" class="notification-badge">
                <el-button circle>
                  <el-icon><Bell /></el-icon>
                </el-button>
              </el-badge>
            </div>
            
            <!-- 用户信息 -->
            <div class="user-info">
              <el-dropdown @command="handleUserCommand">
                <span class="user-dropdown">
                  <el-avatar 
                    :size="36" 
                    :src="avatarUrl" 
                    class="user-avatar"
                  />
                  <div class="user-details" v-if="!isCollapse">
                    <div class="username">{{ user?.nickname || user?.username }}</div>
                    <div class="user-role">管理员</div>
                  </div>
                  <el-icon v-if="!isCollapse"><ArrowDown /></el-icon>
                </span>
                
                <template #dropdown>
                  <el-dropdown-menu class="user-dropdown-menu">
                    <el-dropdown-item command="profile">
                      <el-icon><User /></el-icon>
                      个人资料
                    </el-dropdown-item>
                    <el-dropdown-item command="settings">
                      <el-icon><Setting /></el-icon>
                      系统设置
                    </el-dropdown-item>
                    <el-dropdown-item divided command="logout">
                      <el-icon><SwitchButton /></el-icon>
                      退出登录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        
        <!-- 页面内容 -->
        <el-main class="main-content">
          <transition name="fade-transform" mode="out-in">
            <router-view />
          </transition>
        </el-main>
        
        <!-- 底部 -->
        <el-footer class="footer">
          <div class="footer-content">
            <div class="copyright">
              © 2023 IoT 智能家居管理系统 v1.0
            </div>
            <div class="status">
              <el-tag type="success" size="small">系统运行正常</el-tag>
            </div>
          </div>
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()
const user = ref(null)
const isCollapse = ref(false)
const avatarUrl = ref('https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png')
const onlineDevices = ref(12)
const alarmCount = ref(2)

// 计算属性：当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

// 计算属性：面包屑导航项
const breadcrumbItems = computed(() => {
  const pathMap = {
    '/home': ['首页'],
    '/devices': ['设备管理'],
    '/rooms': ['房间管理'],
    '/scenes': ['场景管理'],
    '/alarms': ['报警信息'],
    '/analytics': ['数据分析'],
    '/settings': ['系统设置']
  }
  return pathMap[route.path] || ['首页']
})

onMounted(() => {
  // 从localStorage获取用户信息
  const userStr = localStorage.getItem('user')
  if (userStr) {
    try {
      user.value = JSON.parse(userStr)
    } catch (e) {
      ElMessage.error('用户信息解析失败，请重新登录')
      logout()
    }
  } else {
    ElMessage.error('未找到用户信息，请重新登录')
    logout()
  }
})

// 处理Logo加载错误
const handleLogoError = () => {
  // Logo加载失败时的处理
  console.log('Logo加载失败，使用默认图标')
}

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 处理用户下拉菜单命令
const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人资料功能开发中')
      break
    case 'settings':
      ElMessage.info('系统设置功能开发中')
      break
    case 'logout':
      ElMessageBox.confirm(
        '确定要退出登录吗？',
        '确认退出',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        logout()
      }).catch(() => {
        // 用户取消退出
      })
      break
  }
}

// 退出登录
const logout = () => {
  localStorage.removeItem('user')
  router.push('/')
}
</script>

<style scoped>
.home-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 背景动画 */
.background-animation {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(-45deg, #1e3c72, #2a5298, #2a5298, #1e3c72);
  background-size: 400% 400%;
  animation: gradientShift 15s ease infinite;
  z-index: -1;
}

@keyframes gradientShift {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* 容器布局 */
.main-container {
  height: 100vh;
  width: 100%;
  max-width: 100%;
  background: transparent;
  margin: 0 auto;
}

.content-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* 侧边栏样式 */
.sidebar {
  background: rgba(48, 65, 86, 0.9);
  backdrop-filter: blur(10px);
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.3);
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(44, 62, 80, 0.8);
  color: white;
  font-size: 18px;
  font-weight: bold;
  transition: all 0.3s ease;
  padding: 0 15px;
}

.logo.collapsed {
  justify-content: center;
}

.logo-image {
  height: 30px;
  margin-right: 10px;
  transition: all 0.3s ease;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  background: linear-gradient(45deg, #409eff, #67c23a);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.sidebar-menu {
  border: none;
  flex: 1;
  overflow-y: auto;
}

.sidebar-menu :deep(.el-menu-item) {
  transition: all 0.3s ease;
}

.sidebar-menu :deep(.el-menu-item:hover) {
  background: rgba(64, 158, 255, 0.2) !important;
  transform: translateX(5px);
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background: rgba(64, 158, 255, 0.3) !important;
  border-left: 4px solid #409eff;
}

.menu-title {
  transition: all 0.3s ease;
}

.sidebar-footer {
  padding: 15px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.system-info {
  color: #bfcbd9;
  font-size: 12px;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-item .el-icon {
  margin-right: 5px;
  font-size: 14px;
}

/* 头部样式 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  height: 60px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
}

.menu-toggle {
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  margin-right: 20px;
  transition: all 0.3s ease;
}

.menu-toggle:hover {
  transform: rotate(90deg);
  color: #409eff;
}

.breadcrumb {
  font-size: 14px;
}

/* 头部右侧 */
.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.notification-area {
  position: relative;
}

.notification-badge :deep(.el-badge__content) {
  background: #f56c6c;
  border: none;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 5px 10px;
  border-radius: 20px;
}

.user-dropdown:hover {
  background: rgba(64, 158, 255, 0.1);
}

.user-avatar {
  margin-right: 10px;
  transition: all 0.3s ease;
}

.user-details {
  margin-right: 10px;
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.user-role {
  font-size: 12px;
  color: #909399;
}

.user-dropdown-menu :deep(.el-dropdown-menu__item) {
  transition: all 0.3s ease;
}

.user-dropdown-menu :deep(.el-dropdown-menu__item:hover) {
  background: rgba(64, 158, 255, 0.1);
  color: #409eff;
}

/* 主内容区域 */
.main-content {
  background: rgba(245, 247, 250, 0.8);
  backdrop-filter: blur(5px);
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  position: relative;
}

/* 底部样式 */
.footer {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  height: 50px !important;
  padding: 0;
}

.footer-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
  font-size: 12px;
  color: #909399;
}

/* 动画效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all 0.3s ease;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(-30px);
}
</style>